package com.fysports.web.controller.engine;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.ControllerException;
import com.fysports.web.kit.commons.StringKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.model.BaseModel;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;


/**
 * Class title: 泛型用于处理简单的curd操作  <br/>
 * v1:基础方法(add,save,edit,update,delete)
 * Describe:                    <br/>
 * Created by konbluesky           <br/>
 * Date : 15/12/28 下午12:20       <br/>
 * Project : weixin    <br/>
 */
public abstract class AutoRestFulController<M extends BaseModel> extends AbstractBaseController
        implements RestFulController {
    private static Log logger = Log.getLog(AutoRestFulController.class);
    public void index() {
        moduleFactory.create(this);
        execute();
        setAttr("list", getCtrModel().searchAll());
        render(getAdminPath(getRenderPath() + "list.ftl"));
    }


    /**
     * dtgrid ajax加载访问方法
     */
    public void load() {
        DTGridView view = getGridView();
        Page<M> list =
                getCtrModel().DTSearchPaginate(view.getNowPage(), view.getPageSize(), null, null, null, null);
        renderJson(toGridView(view, list));
    }


    @Override public void add() {
        createToken();
        try {
            //新增时直接新建实例(方便生成主键)
            setAttr("m", getCtrModel().getClass().newInstance());
        }
        catch (InstantiationException e) {
            logger.error(e.getLocalizedMessage(),e);
            throw new ControllerException(e);
        }
        catch (IllegalAccessException e) {
            logger.error(e.getLocalizedMessage(),e);
            throw new ControllerException(e);
        }
        render(getAdminPath(getRenderPath() + "add.ftl"));
    }


    @Override public void save() {
        if(!checkFormToken()) return;
        BaseModel ur = getModel(getCtrModel().getClass(), "m");
        boolean status = ur.save();
        renderJson(new JsonView(status, status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL));
    }


    @Override public void edit() {
        createToken();
        setAttr("m", ((BaseModel) getCtrModel()).findById(getPara()));
        render(getAdminPath(getRenderPath() + "update.ftl"));
    }


    @Override public void update() {
        if(!checkFormToken()) return;
        BaseModel ur = getModel(getCtrModel().getClass(), "m");
        boolean status = ur.update();
        renderJson(new JsonView(status, status ? OperateType.UPDATE_SUCCESS : OperateType.UPDATE_FAIL));
    }


    @Override public void delete() {
        String ids = getPara("ids");
        boolean status = false;
        if (!StringKit.isBlank(ids)) {
            status = getCtrModel().deletebatch(ids);
        }
        else {
            status = getCtrModel().findById(getPara()).delete();
        }
        renderJson(new JsonView(status, status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL));
    }

    protected abstract M getCtrModel();
    protected abstract String getRenderPath();
}
